package com.clps.loadDictory;

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
//import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.StreamUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

@Component
public class DataInitializer implements ApplicationListener<ContextRefreshedEvent>{

//    private final RedisTemplate<String, String> redisTemplate;
//
//    public RedisDataLoader(RedisTemplate<String, String> redisTemplate) {
//        this.redisTemplate = redisTemplate;
//    }

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        loadPropertiesToRedis();
    }

    private void loadPropertiesToRedis() {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            Resource resource = resolver.getResource("classpath:ENUM_DICTIONARY.txt");
            BufferedReader reader =  new BufferedReader(new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)){
                String line;

            };
        } catch (IOException e) {
            throw new RuntimeException("Error reading ENUM_DICTIONARY.txt file", e);
        }
//        try {
//            Resource[] resources = resolver.getResources("classpath:ENUM_DICTIONARY.txt");
//            for (Resource resource : resources) {
//                EncodedResource encodedResource = new EncodedResource(resource, "UTF-8");
//                Properties properties = PropertiesLoaderUtils.loadProperties(encodedResource);
//
//              //  properties.load(StreamUtils.inputStreamToString(resource.getInputStream(), StandardCharsets.UTF_8).getBytes(StandardCharsets.UTF_8));
//                for (String key : properties.stringPropertyNames()) {
//                    String value = properties.getProperty(key);
//                    System.out.println("value = " + value);
//                //    redisTemplate.opsForValue().set(key, value);
//                }
//            }
//        } catch (IOException e) {
//            throw new RuntimeException("Failed to load properties from resource", e);
//        }
    }
}
